    //
//  HomeViewController_iPad.m
//  SecretFolder
//
//  Created by Deepak Kumar on 23/06/12.
//  Copyright 2012 Frog. All rights reserved.
//

#import "HomeViewController_iPad.h"
#import "CustomTableViewCell.h"
#import "CarouselViewController.h"
#import "SettingViewController.h"
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

#import "Repository.h"
#import "RepositoryObject.h"
#import "Directory.h"

@implementation HomeViewController_iPad
@synthesize mTableView;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	[self loadDocumentDirectory];
    NSDictionary *size = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Helvetica" size:19],UITextAttributeFont, nil];
    self.navigationController.navigationBar.titleTextAttributes = size;

}

- (void) viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:YES];
}
 
- (void) loadDocumentDirectory
{
//	if (mObjectArray) {
//		mObjectArray = nil;
//	}
//	
	// directoryContentsAtPath
//    if ([[NSUserDefaults standardUserDefaults] boolForKey:kDecoyPasswordEntered]== YES) {
//        mObjectArray = nil;
////        [[NSUserDefaults standardUserDefaults] removeObjectForKey:kDecoyPasswordEntered];
//    }
//    else
    {
	mObjectArray = [[NSMutableArray alloc] initWithArray:[[Repository sharedRepository] fetchAllDirectories:nil]];
	}
	[self.mTableView reloadData];
}

- (IBAction)folderTap:(UIButton *)sender {
	Directory *tDirectory = [mObjectArray objectAtIndex:sender.tag];
	//NSLog(@"tCard:%@", tCard.mName);
	
    if (IS_IPAD) {
        CarouselViewController *tController = [[CarouselViewController alloc] initWithNibName:@"CarouselViewController" bundle:nil];
        [tController setDirectory:tDirectory];
        [self.navigationController pushViewController:tController animated:YES];
    }
    else
    {
	CarouselViewController *tController = [[CarouselViewController alloc] initWithNibName:@"CarouselViewController_iPhone" bundle:nil];
	[tController setDirectory:tDirectory];
	[self.navigationController pushViewController:tController animated:YES];
    }
	//[tController release];
    
}

- (IBAction) addFolderButton:(id)sender
{
	AddFolderViewController *controller = [[AddFolderViewController alloc] initWithNibName:@"AddFolderViewController" bundle:nil];
	[controller setMDelegate:self];
	// Create a Navigation controller   
	UINavigationController *navController = [[UINavigationController alloc]
											 initWithRootViewController:controller];
	
	navController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
	navController.modalPresentationStyle = UIModalPresentationFormSheet;
	//navController.navigationBarHidden = YES;
	// show the navigation controller modally
	[self presentViewController:navController animated:YES completion:^{
        
    }];
    

	
	// Clean up resources
}

- (void) addFolderViewController:(AddFolderViewController *)pController refresh:(BOOL)pBool
{
	if (pBool) {
		[self loadDocumentDirectory];
	}
}

- (IBAction) settingButton:(id)sender
{
	SettingViewController *controller = [[SettingViewController alloc] initWithNibName:@"SettingViewController" bundle:nil];

	// Create a Navigation controller   
	UINavigationController *navController = [[UINavigationController alloc]
											 initWithRootViewController:controller];
	
	navController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
	navController.modalPresentationStyle = UIModalPresentationFormSheet;
	//navController.navigationBarHidden = YES;
	// show the navigation controller modally
    if ([[NSUserDefaults standardUserDefaults] boolForKey:kDecoyPasswordEntered] == YES)  {
        
    }
    else
    {
        [self presentViewController:navController animated:YES completion:^{
            
        }];
    }
	
	// Clean up resources
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
	NSInteger offset = [mObjectArray count]%4;
	
	NSLog(@"mObjectArray:%d|offset:%d", [mObjectArray count], offset);
	if (offset == 1) {
		offset = offset + 2;
	}
	
	if ([mObjectArray count] > 0 && [mObjectArray count] < 4) {
		return 1;
	}
    return ([mObjectArray count] + offset)/4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (IS_IPAD) {
        return 180.0f;
    }
    else
    {
        return 80.0f;
    }
	
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *CellIdentifier = @"CustomerCell"; // name of the cell
    
    CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	
	NSArray *topLevelObjects;
	if (cell == nil) {
        if (IS_IPAD) {
            topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell_iPad" owner:self options:nil]; //name of the nib - CustomCell.xib
            cell =  (CustomTableViewCell *)[topLevelObjects objectAtIndex:0];
        }
        else
        {
            topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell_iphone" owner:self options:nil]; //name of the nib - CustomCell.xib
            cell =  (CustomTableViewCell *)[topLevelObjects objectAtIndex:0];
        }
		
    }
    else{
        cell.mTitleLabel1.hidden = NO;
        cell.mSwitch1.hidden = NO;
		
        cell.mTitleLabel2.hidden = NO;
        cell.mSwitch2.hidden = NO;
        
        cell.mTitleLabel3.hidden = NO;
        cell.mSwitch3.hidden = NO;
        
        cell.mTitleLabel4.hidden = NO;
        cell.mSwitch4.hidden = NO;
    }
	
	Directory *tCard = nil;

    if ([mObjectArray count]>indexPath.row*4) {
        tCard = [mObjectArray objectAtIndex:indexPath.row*4];
    }
	
	// C-1
	cell.mTitleLabel1.text = tCard.mName;
	[cell.mSwitch1 setImage:[UIImage imageNamed:[NSString stringWithFormat:@"IT_Folder%d.png", [tCard.mFolderType integerValue]]] forState:0];
	[cell.mSwitch1 addTarget:self action:@selector(folderTap:) forControlEvents:UIControlEventTouchUpInside];
	[cell.mSwitch1 setTag:indexPath.row*4];

	// C-2
	if ([mObjectArray count] > indexPath.row*4+1) {
		tCard = [mObjectArray objectAtIndex:indexPath.row*4+1];
		cell.mTitleLabel2.text = tCard.mName;

        [cell.mSwitch2 setImage:[UIImage imageNamed:[NSString stringWithFormat:@"IT_Folder%d.png", [tCard.mFolderType integerValue]]] forState:0];

		[cell.mSwitch2 setTag:indexPath.row*4+1];
		[cell.mSwitch2 addTarget:self action:@selector(folderTap:) forControlEvents:UIControlEventTouchUpInside];
	}
	else {
		[cell.mTitleLabel2 setHidden:YES];
		[cell.mSwitch2 setHidden:YES];
	}
	
	// C-3
	if ([mObjectArray count] > indexPath.row*4+2) {
		tCard = [mObjectArray objectAtIndex:indexPath.row*4+2];

		cell.mTitleLabel3.text = tCard.mName;
        [cell.mSwitch3 setImage:[UIImage imageNamed:[NSString stringWithFormat:@"IT_Folder%d.png", [tCard.mFolderType integerValue]]] forState:0];
        
		[cell.mSwitch3 setTag:indexPath.row*4+2];
		
		[cell.mSwitch3 addTarget:self action:@selector(folderTap:) forControlEvents:UIControlEventTouchUpInside];
	}
	else {
		[cell.mTitleLabel3 setHidden:YES];
		[cell.mSwitch3 setHidden:YES];
	}
	
	// C-4
	if ([mObjectArray count] > indexPath.row*4+3) {
		tCard = [mObjectArray objectAtIndex:indexPath.row*4+3];

		cell.mTitleLabel4.text = tCard.mName;
        [cell.mSwitch4 setImage:[UIImage imageNamed:[NSString stringWithFormat:@"IT_Folder%d.png", [tCard.mFolderType integerValue]]] forState:0];

		[cell.mSwitch4 setTag:indexPath.row*4+3];
		[cell.mSwitch4 addTarget:self action:@selector(folderTap:) forControlEvents:UIControlEventTouchUpInside];

	}
	else {
		[cell.mTitleLabel4 setHidden:YES];
		[cell.mSwitch4 setHidden:YES];
	}
	cell.textLabel.font = [UIFont fontWithName:@"Helvetica Light" size:15];
    return cell;
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Override to allow orientations other than the default portrait orientation.
    return UIInterfaceOrientationIsLandscape(interfaceOrientation)? YES : NO;
}



- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}




@end
